/*******************************************************************************
 * Copyright 2010 DemandSpot
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.demandspot.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.Messages;
import com.google.gwt.i18n.client.LocalizableResource.DefaultLocale;

/**
 * Utilities that express time difference in words.
 * Shamelessly translated from Rails' ActionView::Helpers::DateHelper
 * @author elad
 */
public class TimeDiffUtils {

	@DefaultLocale("en")
	public interface Labels extends Messages {
		@DefaultMessage("{0} minutes")
		@PluralText({"none", "less than a minute", "one", "a minute"})
		String xMinutes(@PluralCount int minutes);

		@DefaultMessage("about {0} hours")
		@PluralText({"one", "about an hour"})
		String xHours(@PluralCount int hours);

		@DefaultMessage("{0} days")
		@PluralText({"one", "about one day"})
		String xDays(@PluralCount int days);

		@DefaultMessage("{0} months")
		@PluralText({"one", "about one month"})
		String xMonths(@PluralCount int months);
		
		@DefaultMessage("over {0} years")
		@PluralText({"one", "about one year"})
		String xYears(@PluralCount int years);

		@DefaultMessage("ago")
		String pastTimeExt();		
	}
	
	private static final Labels labels = GWT.create(Labels.class);
	
	/**
	 * Provides a word representation of the approximate distance in time between two times.
	 * If fromMillis is larger than toMillis, adds "ago" to the description.
	 */
	public static String distanceOfTimeInWords(long fromMillis, long toMillis) {		
		String message;

		// difference in minutes
		long diff = Math.abs(toMillis - fromMillis) / 60000;

		if (diff < 45)
			message = labels.xMinutes((int) diff);
		else if (diff < 90)
			message = labels.xHours(1);
		else if (diff < 1440)
			message = labels.xHours((int) (diff / 60));
		else if (diff < 2880)
			message = labels.xDays(1);
		else if (diff < 43200)
			message = labels.xDays((int) (diff / 1440));
		else if (diff < 86400)
			message = labels.xMonths(1);
		else if (diff < 525600)
			message = labels.xMonths((int) (diff / 43200));
		else if (diff < 1051200)
			message = labels.xYears(1);
		else
			message = labels.xYears((int) (diff / 525600));

		// add "ago" extension to times in the past
		if (fromMillis > toMillis)
			message += " " + labels.pastTimeExt();

		return message;
	}

	public static String distanceOfTimeInWords(java.util.Date fromDate, java.util.Date toDate) {
		return distanceOfTimeInWords(fromDate.getTime(), toDate.getTime());
	}

	public static String timeAgoInWords(java.util.Date date) {
		return distanceOfTimeInWords(System.currentTimeMillis(), date.getTime());
	}

}
